using IQIGame.Onigao.Framework;
using IQIGame.Onigao.Game;
using IQIGame.Onigao.GameAOT;

namespace IQIGame.Onigao.GamePlay
{
    public class UIN_CommonSelectUnit : UIBaseNode
    {
        #region Template Generate,don't modify
        protected class UIB_UIN_CommonSelectUnit
        {
            #region ObjectBinding Generate
            public IQIGame.Onigao.Framework.ExButton btnMin { protected set; get; }
            public IQIGame.Onigao.Framework.ExButton btnAdd { protected set; get; }
            public IQIGame.Onigao.Framework.ExButton btnReduce { protected set; get; }
            public IQIGame.Onigao.Framework.ExButton btnMax { protected set; get; }
            public IQIGame.Onigao.Framework.ExText text_01 { protected set; get; }
            public virtual void InitBinding(ObjectBinding __binding)
            {
                __binding.TryGetVariableValue<IQIGame.Onigao.Framework.ExButton>("btnMin", out var __tbv0);
                this.btnMin = __tbv0;
                __binding.TryGetVariableValue<IQIGame.Onigao.Framework.ExButton>("btnAdd", out var __tbv1);
                this.btnAdd = __tbv1;
                __binding.TryGetVariableValue<IQIGame.Onigao.Framework.ExButton>("btnReduce", out var __tbv2);
                this.btnReduce = __tbv2;
                __binding.TryGetVariableValue<IQIGame.Onigao.Framework.ExButton>("btnMax", out var __tbv3);
                this.btnMax = __tbv3;
                __binding.TryGetVariableValue<IQIGame.Onigao.Framework.ExText>("text_01", out var __tbv4);
                this.text_01 = __tbv4;
            }
            #endregion ObjectBinding Generate 
        }
        #endregion Template Generate,don't modify

        #region fields
        private int MaxNum;
        private int MinNum;
        private System.Action<int> callOnNumChanged;
        #endregion fields

        #region properties


        protected UIB_UIN_CommonSelectUnit ui { get; set; }
        protected UIMsgDispatcher uiMsgDispatcher => this._msgDispatcher as UIMsgDispatcher;


        public int SelectNum { get; private set; }//选择的数量
        #endregion properties

        protected override void BeforeInit()
        {
            ui = new UIB_UIN_CommonSelectUnit();
            ui.InitBinding(this.csObjBind);
        }

        protected override void OnInit()
        {
            this.ui.btnAdd.AddClickEvent(this.OnBtnAdd_Clicked);
            this.ui.btnMin.AddClickEvent(this.OnBtnMin_Clicked);
            this.ui.btnMax.AddClickEvent(this.OnBtnMax_Clicked);
            this.ui.btnReduce.AddClickEvent(this.OnBtnReduce_Clicked);
        }

        protected override void OnShow()
        {

        }

        protected override void OnHide()
        {

        }

        /// <summary>
        /// 添加UGUI侦听
        /// </summary>
        protected override void AddUGUIListener()
        {

        }

        /// <summary>
        /// 添加全局侦听（MsgDispatcher）
        /// </summary>
        protected override void AddMsgListeners()
        {

        }

        protected override void OnDispose()
        {

        }

        #region methonds

        public void Show(int CurSelectNum, int MinNum, int MaxNum, System.Action<int> funcNumChanged)
        {
            this.MaxNum = MaxNum;
            this.MinNum = MinNum;
            this.SelectNum = CurSelectNum;
            this.callOnNumChanged = funcNumChanged;
            this.SetCurSelectNum(CurSelectNum);
            this.Show();
        }


        void SetCurSelectNum(int curSelectNum)
        {
            bool isChanged = this.SelectNum != curSelectNum;
            this.SelectNum = curSelectNum;
            this.ui.text_01.text = this.SelectNum.ToString();
            this.ui.btnAdd.SetActive(this.SelectNum < this.MaxNum);
            this.ui.btnReduce.SetActive(this.SelectNum > this.MinNum);
            this.ui.btnMin.SetActive(this.SelectNum != this.MinNum);
            this.ui.btnMax.SetActive(this.SelectNum != this.MaxNum);

            if (isChanged)
            {
                this.callOnNumChanged?.Invoke(curSelectNum);
            }
        }

        void OnBtnAdd_Clicked()
        {
            int nTempNum = this.SelectNum + 1;
            if (nTempNum > this.MaxNum) return;
            this.SetCurSelectNum(nTempNum);
        }
        void OnBtnReduce_Clicked()
        {
            int nTempNum = this.SelectNum - 1;
            if (nTempNum < this.MinNum) return;
            this.SetCurSelectNum(nTempNum);
        }

        void OnBtnMin_Clicked()
        {
            if (this.MinNum == this.SelectNum) return;
            this.SetCurSelectNum(this.MinNum);
        }

        void OnBtnMax_Clicked()
        {
            if (this.MaxNum == this.SelectNum) return;
            this.SetCurSelectNum(this.MaxNum);
        }
        #endregion methonds
    }
}
